/**
 * 版权所有(C)，中国银联股份有限公司，2002-2013，所有权利保留。
 * 
 * 项目名：	simple-web
 * 文件名：	SystemInfoAction.java
 * 模块说明：	
 * 修改历史：
 * 2013-11-1 - Administrator - 创建。
 */
package com.aeky.simple.action.systemInfo;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.aeky.simple.action.BaseAction;
import com.aeky.simple.cache.CacheManager;
import com.aeky.simple.entity.SystemInfo;
import com.aeky.simple.enums.EnableStatusEnum;
import com.aeky.simple.service.SystemInfoService;
import com.aeky.simple.util.DateUtil;
import com.aeky.simple.vo.ajax.ComboxVO;
import com.aeky.simple.vo.page.Pager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.opensymphony.xwork2.Action;

/**
 * 系统信息Action
 * 
 * @author wanghui
 * 
 */
public class SystemInfoAction extends BaseAction {

    private static final long serialVersionUID = -1458175482094286154L;

    @Autowired
    private SystemInfoService systemInfoService;

    /** 业务系统信息 */
    private SystemInfo systemInfo;

    /**
     * 初始化系统信息管理页面
     * 
     * @return
     * @throws Exception
     */
    public String initSystemInfo() throws Exception {
        return Action.SUCCESS;
    }

    /**
     * 初始化新增业务系统页面
     * 
     * @return
     * @throws Exception
     */
    public String initAddSystemInfo() throws Exception {
        return "initAdd";
    }

    /**
     * 分页查询系统信息
     * 
     * @return
     * @throws Exception
     */
    public String listSystemInfo() throws Exception {
        final String sysCode = request.getParameter("sysCode");
        final String sysName = request.getParameter("sysName");
        Pager<SystemInfo> pager = new Pager<SystemInfo>(request.getParameter("page"),
                        request.getParameter("rows"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("sysCode", sysCode);
        dataMap.put("sysName", sysName);
        try {
            pager = systemInfoService.querySystemInfoList(pager, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult = this.getJsonFromObject(changeToMapPager(pager));
        return AJAX_RESULT;
    }

    public <T> void addOtherMapValue(T t, Map<String, Object> map) {
        SystemInfo si = (SystemInfo) t;
        map.put("sysStatusName", EnableStatusEnum.getEnumNameByValue(si.getSysStatus()));
        map.put("recordCrtTime", DateUtil.formatDateDefaultDisplay(si.getRecordCrtTime()));
        map.put("recordUpdTime", DateUtil.formatDateDefaultDisplay(si.getRecordUpdTime()));
    }

    /**
     * 新增系统信息
     * 
     * @return
     * @throws Exception
     */
    public String addSystemInfo() throws Exception {
        systemInfo.setRecordCrtOperLoginName(this.getLoginUserName());
        systemInfo.setRecordUpdOperLoginName(this.getLoginUserName());
        try {
            systemInfoService.addSystemInfo(systemInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CacheManager.refreshSystemInfoCache();
        ajaxResult = buildJSONResponse("success", "业务系统添加成功！");
        return AJAX_RESULT;
    }

    /**
     * 删除系统信息
     * 
     * @return
     * @throws Exception
     */
    public String deleteSystemInfo() throws Exception {
        final String str = request.getParameter("sysCodes");
        final List<String> sysCodeList = Arrays.asList(str.split("\\|"));
        try {
            systemInfoService.deleteSystemInfo(sysCodeList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CacheManager.refreshSystemInfoCache();
        ajaxResult = buildJSONResponse("success", "操作成功");
        return AJAX_RESULT;
    }

    /**
     * 初始化编辑页面
     * 
     * @return
     * @throws Exception
     */
    public String initEditSystemInfo() throws Exception {
        final String sysCode = request.getParameter("sysCode");
        final Integer version = Integer.valueOf(request.getParameter("version"));
        SystemInfo si = null;
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("sysCode", sysCode);
            map.put("version", version);
            si = systemInfoService.querySystemInfoBySystemCodeAndVersion(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (si == null) {
            ajaxResult = buildJSONResponse("fail", "数据版本有变，将刷新页面！");
        } else {
            ajaxResult = buildJSONResponse("success",
                            this.getJsonFromObject(converterObjectToMap(si)));
        }
        return AJAX_RESULT;
    }

    /**
     * 更新系统信息
     * 
     * @return
     * @throws Exception
     */
    public String editSystemInfo() throws Exception {
        final String str = request.getParameter("sysInfo");
        Gson gson = new Gson();
        Type type = new TypeToken<SystemInfo>() {
        }.getType();
        SystemInfo si = gson.fromJson(str, type);
        si.setRecordUpdOperLoginName("222222");
        try {
            systemInfoService.updateSystemInfo(si);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CacheManager.refreshSystemInfoCache();
        ajaxResult = buildJSONResponse("success", "操作成功");
        return AJAX_RESULT;
    }

    /**
     * 更改系统信息状态
     * 
     * @return
     * @throws Exception
     */
    public String changeSystemInfoStatus() throws Exception {
        final String sysCode = request.getParameter("sysCode");
        final String sysStatus = request.getParameter("sysStatus");
        final String version = request.getParameter("version");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sysCode", sysCode);
        map.put("recordUpdOperLoginName", "333");
        map.put("sysStatus", sysStatus);
        map.put("version", version);
        try {
            systemInfoService.changeSystemInfoStatus(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CacheManager.refreshSystemInfoCache();
        ajaxResult = buildJSONResponse("success", "操作成功");
        return AJAX_RESULT;
    }

    /**
     * 验证系统编号是否存在
     * 
     * @return
     * @throws Exception
     */
    public String checkSysCode() throws Exception {
        final String sysCode = request.getParameter("sysCode");
        SystemInfo si = null;
        try {
            si = systemInfoService.querySystemInfoBySystemCode(sysCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (si != null) {
            ajaxResult = buildJSONResponse("fail", "该系统编号已存在，请重新输入！");
        } else {
            ajaxResult = buildJSONResponse("success", "该系统编号可用！");
        }
        return AJAX_RESULT;
    }

    /**
     * 得到业务系统下拉列表值
     * 
     * @return
     * @throws Exception
     */
    public String getSysCodeComboxValue() throws Exception {
        List<SystemInfo> list = null;
        try {
            list = systemInfoService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ComboxVO> comboVOList = new ArrayList<ComboxVO>();
        ComboxVO vo = new ComboxVO("", "请选择");
        comboVOList.add(vo);
        if (list != null && !list.isEmpty()) {
            for (SystemInfo si : list) {
                vo = new ComboxVO(si.getSysCode(), si.getSysName());
                comboVOList.add(vo);
            }
        }
        ajaxResult = this.getJsonFromObject(comboVOList);
        return AJAX_RESULT;
    }

    public SystemInfo getSystemInfo() {
        return systemInfo;
    }

    public void setSystemInfo(SystemInfo systemInfo) {
        this.systemInfo = systemInfo;
    }

}
